home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro14 / errors1.bas < prev    next >
Encoding:
BASIC Source File  |  1990-12-10  |  3.9 KB  |  78 lines

  1. 10 'ERRORS1.BAS - Demonstrating how YOU can trap errors in a BASIC program
  2. 20 'From the GW-BASIC Tutorial Series, GWBT07, 12/15/1990
  3. 30 '
  4. 40 'Variables used in this program:
  5. 50 '
  6. 60 '    ERR - provided by BASIC to hold the error code
  7. 70 '    ERL - provided by BASIC to hold the error line number
  8. 80 '
  9. 90 'Start of main program:
  10. 100 ON ERROR GOTO 1000
  11. 110 CLS:KEY OFF
  12. 120 PRINT "I am going to show you how you can prevent errors from happening in"
  13. 130 PRINT "your BASIC programs."
  14. 140 PRINT
  15. 150 PRINT "The first error we're going to demonstrate is what happens when you"
  16. 160 PRINT "leave a disk drive door open.  Please take any disk out of your A:"
  17. 170 PRINT "drive, and press a key when you have done this..."
  18. 180 'Pause until user presses a key...
  19. 190 WHILE INKEY$="":WEND
  20. 200 FILES "A:\"
  21. 210 PRINT "You should have seen a message that there was no diskette in your A:"
  22. 220 PRINT "drive.  Now that you've seen this, let's see what happens when we"
  23. 230 PRINT "try to read a file that is not on your diskette."
  24. 240 PRINT "Press a key when there is a diskette in your A: drive..."
  25. 250 WHILE INKEY$="":WEND
  26. 260 OPEN "A:\DATA.DAT" FOR INPUT AS #1
  27. 270 PRINT "You should have seen the message that a needed file could not be "
  28. 280 PRINT "found."
  29. 290 PRINT "Next, let's look at what happens when you try to use a printer that"
  30. 300 PRINT "is not ready.  If your printer is ON, please turn it OFF."
  31. 310 PRINT "Press any key when your printer is OFF..."
  32. 320 'Again, wait for a keypress...
  33. 330 WHILE INKEY$="":WEND
  34. 340 LPRINT "Hello there!"
  35. 350 PRINT "You'll note that this time, the program did NOT continue, but waited "
  36. 360 PRINT "until you had fixed the printer and it was ready to print our message."
  37. 370 PRINT
  38. 380 PRINT "If you examine the listing of the error handling routine (lines 1000"
  39. 390 PRINT "and up), you'll see the difference."
  40. 400 PRINT "The program is now ending.  Thanks for your cooperation!"
  41. 410 'NOTE that you must have an END statement before your error handling routine
  42. 420 'starts, otherwise BASIC will happily run into it, and cause you many
  43. 430 'headaches later in life!
  44. 440 END
  45. 1000 'Beginning of error handling routine.  BASIC will give us the ERR variable
  46. 1010 'containing the Error Code, and ERL containing the line number where the
  47. 1020 'error happened.  In this demonstration, we don't need to use the ERL
  48. 1030 'value, but the ERR will come in handy...
  49. 1040 '
  50. 1050 'The error code for a printer error, depending on your printer/port, could
  51. 1060 'be either 24 (Device Timeout), 25 (Device Fault) or 27 (Out of Paper), so
  52. 1070 'let's set our first 'trap' to catch these...
  53. 1080 IF ERR=24 OR ERR=25 OR ERR=27 THEN BEEP:PRINT:PRINT "Printer is not ready!  Press a key to continue...":PRINT:WHILE INKEY$="":WEND:RESUME
  54. 1090 'The RESUME statement tells BASIC to go back to the line that the error happened in, and try it again (hopefully the error has been corrected)
  55. 1100 '
  56. 1110 'The error code for a disk drive not ready is 71 (Disk not Ready).  Let's
  57. 1120 'stop it from happening...
  58. 1130 IF ERR=71 THEN BEEP:PRINT:PRINT "Disk drive A: is not ready!  Continuing with program...":PRINT:RESUME NEXT
  59. 1140 '
  60. 1150 'Notice the RESUME NEXT - this tells BASIC to go to the next line in the
  61. 1160 'program to continue after the error.
  62. 1170 '
  63. 1180 'The error for a file not found is 53 (File not Found).  Let's stop this
  64. 1190 'error from happening...
  65. 1200 IF ERR=53 THEN BEEP:PRINT:PRINT "Requested file was not found on the diskette!  Continuing with program...":PRINT:RESUME 290
  66. 1210 'Note that in this case we used RESUME with a line number.  You CAN do this
  67. 1220 'if you know exactly where to go if an error happens.
  68. 1230 '
  69. 1240 'The final lines are for any errors that we didn't trap...
  70. 1250 ON ERROR GOTO 0
  71. 1260 'If an error happens that we didn't set up a trap for, it will print the
  72. 1270 'error message and end the program.
  73. 1280 '
  74. 1290 'End of error handling section
  75. 1300 '
  76. 1310 'End of program - ERRORS1.BAS
  77. 
  78.